package com.zbkj.common.exception;

import com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException;
import com.zbkj.common.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.Objects;

/**
 * 全局参数、异常拦截
 * +----------------------------------------------------------------------
 * | Author:  
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestControllerAdvice
@Order(-1)
public class GlobalExceptionHandler {
//    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 拦截表单参数校验
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({BindException.class})
    public CommonResult bindException(BindException e) {
        log.error("BindException ",e);
        //BindingResult bindingResult = e.getBindingResult();
        return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED);
    }

    /**
     * 拦截JSON参数校验
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResult bindException(MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException ",e);
        BindingResult bindingResult = e.getBindingResult();
        return CommonResult.failed(ExceptionCodeEnum.VALIDATE_FAILED,Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
    }

    /**
     * 拦截参数类型不正确
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public CommonResult bindException(HttpMediaTypeNotSupportedException e){
        log.error("HttpMediaTypeNotSupportedException e", e);
        return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH,Objects.requireNonNull(e.getMessage()));
    }


    //声明要捕获的异常
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public <T> CommonResult<?> defaultExceptionHandler(Exception e) {
        log.error("异常:{}",e);
        e.printStackTrace();
        if(e instanceof CommonException) {
            return CommonResult.failed(ExceptionCodeEnum.FAILED,Objects.requireNonNull(e.getMessage()));
        }
        if(e instanceof MissingServletRequestParameterException){
            return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH, Objects.requireNonNull(e.getMessage()));
        }
        //未知错误
        return CommonResult.failed(ExceptionCodeEnum.ERROR,ExceptionCodeEnum.ERROR.getMessage());
    }

    /**
     * 捕获限流的异常
     * @param biz
     * @return
     */
    @ExceptionHandler(BizExecption.class)
    public <T> CommonResult<?>  limiterException(BizExecption biz){
        return CommonResult.failed(biz.getMsg());
    }

    /**
     * 自定义异常 失败code和失败信息
     * @param re
     * @param <T>
     * @return
     */
    @ExceptionHandler(ResultException.class)
    public <T> CommonResult<?>  resultCodeException(ResultException re){
        return CommonResult.failed(re);
    }

    /**
     * 参数类型不匹配
     *
     * @param exception
     * @return
     * @param <T>
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public <T> CommonResult<?>  methodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception){
        log.error("MethodArgumentTypeMismatchException" ,exception);
        return CommonResult.failed(ExceptionCodeEnum.PRAM_NOT_MATCH);
    }

    @ExceptionHandler(MySQLTransactionRollbackException.class)
    public <T> CommonResult<?>  MySQLTransactionRollbackException(MySQLTransactionRollbackException exception){
        log.error("MySQLTransactionRollbackException" ,exception);
        return CommonResult.failed(ExceptionCodeEnum.FAILED);
    }


}
